<template>
	<view class="register-container">
		<!-- Logo -->
		<view class="logo-container">
			<image src="/static/u408.png" class="logo" mode="aspectFit"></image>
		</view>

		<!-- 标题 -->
		<view class="title-container">
			<text class="title">注册医生账号</text>
			<text class="subtitle">欢迎使用家医</text>
		</view>

		<!-- 表单 -->
		<view class="form-container">
			<!-- 手机号输入 -->
			<view class="input-group">
				<text class="input-label">手机号</text>
				<view class="input-container">
					<text class="country-code">+86</text>
					<input class="input-field" type="number" placeholder="请输入手机号" v-model="phone" maxlength="11" />
				</view>
			</view>



			<!-- 密码输入 -->
			<view class="input-group">
				<text class="input-label">密码</text>
				<view class="input-container">
					<input class="input-field" type="password" placeholder="请输入密码" v-model="password" />
				</view>
			</view>

			<!-- 用户协议 -->
			<view class="agreement-container">
				<checkbox-group @change="onAgreementChange">
					<label class="agreement-label">
						<checkbox value="agree" :checked="isAgreed" color="#4A90E2" />
						<text class="agreement-text">
							我已阅读并同意
							<text class="agreement-link" @click="showUserAgreement">《用户隐私政策》</text>
						</text>
					</label>
				</checkbox-group>
			</view>

			<!-- 注册按钮 -->
			<button class="register-btn" @click="handleRegister" :disabled="!canRegister">
				立即注册
			</button>

			<!-- 底部链接 -->
			<view class="bottom-links">
				<text class="link-text" @click="goToLogin">已有账号，去登录</text>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, computed } from 'vue'
import { registerApi } from '@/api/login.js'

// 响应式数据
const phone = ref('')
const password = ref('')
const isAgreed = ref(false)

// 计算属性
const canRegister = computed(() => {
	return phone.value.length === 11 &&
		password.value.length > 0 &&
		isAgreed.value
})

// 方法定义
const onAgreementChange = (e) => {
	isAgreed.value = e.detail.value.includes('agree')
}

const showUserAgreement = () => {
	// 显示用户协议页面
	uni.showModal({
		title: '用户隐私政策',
		content: '这里是用户隐私政策的内容...',
		confirmText: '我知道了'
	})
}



const handleRegister = () => {
	if (!canRegister.value) {
		return
	}

	uni.showLoading({
		title: '注册中...'
	})

	registerApi({
		phone: phone.value,
		password: password.value
	}).then(res => {
		uni.hideLoading()
		if (res.code == 200) {
			console.log(res.data)  // 后端返回的数据

			uni.showToast({
				title: '注册成功',
				icon: 'success'
			})

			// 注册成功后跳转到登录页
			setTimeout(() => {
				uni.navigateBack()
			}, 1500)
		} else {
			uni.showToast({
				title: res.message || '注册失败',
				icon: 'none'
			})
		}
	}).catch(err => {
		console.error('注册失败:', err)
		uni.hideLoading()
		uni.showToast({
			title: '注册失败，请重试',
			icon: 'none'
		})
	})
}

const goToLogin = () => {
	uni.navigateBack()
}
</script>

<style scoped>
.register-container {
	min-height: 100vh;
	padding: 0 40rpx;
	position: relative;
}

.logo-container {
	display: flex;
	justify-content: center;
	margin-top: 120rpx;
	margin-bottom: 60rpx;
}

.logo {
	width: 120rpx;
	height: 120rpx;
}

.title-container {
	text-align: center;
	margin-bottom: 80rpx;
}

.title {
	display: block;
	font-size: 48rpx;
	font-weight: 500;
	color: #333;
	margin-bottom: 20rpx;
}

.subtitle {
	display: block;
	font-size: 28rpx;
	color: #999;
}

.form-container {
	width: 100%;
}

.input-group {
	margin-bottom: 40rpx;
}

.input-label {
	display: block;
	font-size: 32rpx;
	color: #333;
	margin-bottom: 20rpx;
}

.input-container {
	background: #fff;
	border-radius: 12rpx;
	padding: 0 24rpx;
	height: 88rpx;
	border: 2rpx solid #e5e5e5;
	display: flex;
	align-items: center;
}

.country-code {
	font-size: 32rpx;
	color: #666;
	margin-right: 20rpx;
	border-right: 2rpx solid #e5e5e5;
	padding-right: 20rpx;
}

.input-field {
	flex: 1;
	font-size: 32rpx;
	color: #333;
}



.agreement-container {
	margin: 40rpx 0;
}

.agreement-label {
	display: flex;
	align-items: flex-start;
}

.agreement-text {
	font-size: 26rpx;
	color: #666;
	margin-left: 16rpx;
	line-height: 1.5;
}

.agreement-link {
	color: #4A90E2;
}

.register-btn {
	width: 100%;
	height: 88rpx;
	background: #4A90E2;
	color: white;
	border: none;
	border-radius: 12rpx;
	font-size: 32rpx;
	font-weight: 500;
	margin: 60rpx 0 40rpx 0;
}

.register-btn:disabled {
	background: #ccc;
}

.bottom-links {
	text-align: center;
	margin-top: 40rpx;
}

.link-text {
	font-size: 28rpx;
	color: #4A90E2;
}
</style>
